package org.apache.lucene.demo;

import java.io.IOException;

import org.apache.lucene.index.AtomicReaderContext;
import org.apache.lucene.index.BinaryDocValues;
import org.apache.lucene.search.Collector;
import org.apache.lucene.search.FieldCache;
import org.apache.lucene.search.Scorer;
import org.apache.lucene.util.BytesRef;

public class GroupCollector extends Collector{
	//保存索引域的值
	private String group1;
	private String group2;
	private BinaryDocValues bdv1=null;
	private BinaryDocValues bdv2=null;
	private GroupValue groupValue=null;
	
	public GroupCollector(GroupValue groupValue,String group1,String group2) {
		this.group1=group1;
		this.group2=group2;
		this.groupValue=groupValue;
	}
	
	@Override
	public boolean acceptsDocsOutOfOrder() {
		//是否顺序返回文档,true为排序，false为无序
		return false;
	}

	@Override
	public void collect(int docid) throws IOException {
		BytesRef br1= bdv1.get(docid);
		BytesRef br2= bdv2.get(docid);
		groupValue.putBayonetMap(br2.utf8ToString(), br1.utf8ToString());
	}

	@Override
	public void setNextReader(AtomicReaderContext atomicReader) throws IOException {
		bdv1 = FieldCache.DEFAULT.getTerms(atomicReader.reader(), group1,true);
		bdv2 = FieldCache.DEFAULT.getTerms(atomicReader.reader(), group2,true);
	}

	@Override
	public void setScorer(Scorer arg0) throws IOException {
	}
	
}
